import java.util.*;

import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;

public class GraphView extends Pane {
    /**
     * 全局变量
     */
    private Graph<? extends Displayable> graph;

    /**
     * 构造方法
     */
    public GraphView(Graph<? extends Displayable> g) {
        this.graph = g;

        //可视化顶点
        List<? extends Displayable> vertices = graph.getVertices();
        for (int i = 0; i < graph.getSize(); i++) {
            int x = vertices.get(i).getX();
            int y = vertices.get(i).getY();
            String name = vertices.get(i).getName();

            getChildren().add(new Circle(x, y, 16));
            getChildren().add(new Text(x - 8, y - 18, name));
        }

        //可视化边
        for (int i = 0; i < graph.getSize(); i++) {
            List<Integer> neighbor = graph.getNeighbors(i);
            int x1 = vertices.get(i).getX();
            int y1 = vertices.get(i).getY();
            for (Integer v : neighbor) {
                int x2 = vertices.get(v).getX();
                int y2 = vertices.get(v).getY();

                getChildren().add(new Line(x1, y1, x2, y2));
            }
        }
    }
}